home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14069 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  44 lines

  1. Path: lrz-muenchen.de!news
  2. From: watzka@stat.uni-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: returning address of value to main
  5. Date: 11 Apr 1996 09:37:05 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4kijs1$je1@sparcserver.lrz-muenchen.de>
  9. References: <4khpjq$a3c@pipe1.nyc.pipeline.com>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11.  
  12. luciferm@nyc.pipeline.com (manjila thapa) writes:
  13.  
  14. >I want to return the address  of element of an array 
  15. >a[0]....a[n]....a[m-1]          (address of a[n] is to be returned)
  16. >from a function to the main so that:
  17.  
  18. >main(){
  19. >double *newarr;
  20. >...
  21. >...
  22. >newarr= function(......) /*function is supposed to return address*/
  23. >__________________________________________
  24. >i tried 
  25.  
  26. >return &a[n];
  27.  
  28. >in function but it doesn't work! Isn't this the right way to get the
  29. >address? (this is part of my hw, hope u don't mind) 
  30.  
  31. "Doesn't work" is a weak description of a problem, imho. "&a[n]" and
  32. "a + n" both give you the address of the n + 1 th element of the
  33. array, but you could have found this information in any textbook
  34. on C. The question is whether the memory location referred by
  35. "a + n" is still valid after you return form your function. This
  36. is not the case for an array with storage class "auto" in a 
  37. function.
  38.  
  39. Kurt
  40. --
  41. | Kurt Watzka                             Phone : +49-89-2180-6254
  42. | watzka@stat.uni-muenchen.de
  43.  
  44.